/**
 * 
 */
package org.lightmtv.dispatcher;

import internal.standard.StandardSettings;

import javax.servlet.ServletContext;

import org.lightcommons.resource.ClassPathResource;
import org.lightcommons.resource.ClassPathResourceLoader;
import org.lightcommons.resource.MultiResourceLoader;
import org.lightcommons.resource.ServletContextResourceLoader;
import org.lightcommons.resource.URLResourceLoader;
import org.lightcommons.template.MultiTemplateFactory;
import org.lightcommons.template.TemplateFactory;
import org.lightcommons.template.commontemplate.CommonTemplateFactory;
import org.lightcommons.template.freemarker.FreeMarkerFactory;
import org.lightcommons.util.Assert;
import org.lightcommons.util.StringUtils;
import org.lightmtv.config.GlobalContext;

/**
 * @author gl
 * @since Jul 2, 2008
 */
public class BootStrap {
	public static void init(ServletContext context){
		GlobalContext.setSettings(StandardSettings.getInstance());
		initTemplateFactory();
		initTemplateLoader(context);
		
		//====after all default initilization.call life cycle.
		GlobalContext.getSettings().getLifeCycle().start();
	}

	private static void initTemplateFactory(){
		MultiTemplateFactory templateFactory =GlobalContext.getSettings().getTemplateFactory();
		if(isClassAvailable("freemarker.template.Template")){
			FreeMarkerFactory ftl = new FreeMarkerFactory();
			templateFactory.put("ftl", ftl);
		}
		if(isClassAvailable("org.commontemplate.core.Template")){
			CommonTemplateFactory ctl;
			if(new ClassPathResource("commontemplate.properties").exists()){
				ctl = new CommonTemplateFactory("commontemplate.properties");
			}else{
				ctl = new CommonTemplateFactory(CommonTemplateFactory.WEB_CONFIG_PATH);
			}
			templateFactory.put("ctl", ctl);
		}
		
		String engineName=GlobalContext.getSettings().getProperty("engine.default");
		if(StringUtils.hasText(engineName)){
			TemplateFactory engine = templateFactory.get(engineName);
			Assert.notNull(engine, "No engine found named:"+engineName);
			templateFactory.setDefaultEngine(engine);
		}
	}
	
	private  static void initTemplateLoader(ServletContext context){
		MultiResourceLoader resourceLoader = GlobalContext.getSettings().getTemplateLoader();
		String templatePath=GlobalContext.getSettings().getProperty("template.path");
		Assert.hasText(templatePath,"template.path has no properties");
		String[] paths=templatePath.split("\\,");
		for(String path:paths){
			path = path.trim();
			if(path.startsWith("classpath://")){
				resourceLoader.addLoader(new ClassPathResourceLoader(path.substring(13)));
			}else if(path.startsWith("webapp://")){
				resourceLoader.addLoader(new ServletContextResourceLoader(context,path.substring(9)));
			}else {
				resourceLoader.addLoader(new URLResourceLoader(path));
			}
		}
	}
	
    private static boolean isClassAvailable(final String className) {
        try {
            Class.forName(className);
            return true;
        } catch (final Throwable t) {
            return false;
        }
    }
}
